package cn.itcast.nio.c1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class TestFileChannelTransferTo {
    public static void main(String[] args) {
        try( FileChannel from =new FileInputStream("data.txt").getChannel();
             FileChannel to =new FileOutputStream("to5.txt").getChannel()
        ) {
            //效率高,底层使用操作系统零拷贝进行优化
            //传输上限2g
            long size = from.size();
//            from.transferTo(0, size,to);

            //当传输的文件大于2g时
            for(long left = size;left>0;){
                left -= from.transferTo(size - left,left,to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
